package ecom.appcode.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecom.appcode.business.CategoryLogic;
import ecom.appcode.business.CategoryLogicImpl;
import ecom.appcode.business.ContactLogic;
import ecom.appcode.business.ContactLogicImpl;
import ecom.appcode.business.ShoesLogic;
import ecom.appcode.business.ShoesLogicImpl;
import ecom.appcode.business.UsersLogic;
import ecom.appcode.business.UsersLogicImpl;
import ecom.appcode.common.constants.CommonConstants;
import ecom.appcode.common.daobean.CategoryDaoBean;
import ecom.appcode.common.daobean.ShoesDaoBean;
import ecom.appcode.common.daobean.ShopDaoBean;
import ecom.appcode.common.daobean.UsersDaoBean;

/**
 * Servlet implementation class ShoesIndexAction
 */
public class ShoesIndexAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShoesIndexAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String btAction = request.getParameter(CommonConstants.VALUE_HIDDEN_BUTTON);
        if (CommonConstants.LOGOUT.equals(btAction)) {
            HttpSession session = request.getSession(true);
            String page = request.getParameter(CommonConstants.VALUE_RETURN_LOGOUT_PAGE);
            session.setAttribute(CommonConstants.EMAIL, CommonConstants.BLANK);
            session.invalidate();
            RequestDispatcher rd = request.getRequestDispatcher(page);
            rd.forward(request, response);
        } else if (CommonConstants.VIEW_PRODUCT.equals(btAction)) {
            String string_shoesID = request.getParameter(CommonConstants.VALUE_SHOE_ID);
            int shoesID = Integer.parseInt(string_shoesID);
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            ShoesDaoBean shoesBean = shoesLogic.loadAShoesProduct(shoesID);
            if (shoesBean != null) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SHOES_PRODUCT, shoesBean);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.VIEW_SHOES_PAGE);
                rd.forward(request, response);
            }

        } else if (CommonConstants.LOAD_NEW_ARRIVALS_CATEGORY.equals(btAction)) {
            String string_categoryID = request.getParameter(CommonConstants.VALUE_CATEGORY_ID);
            String categoryName = request.getParameter(CommonConstants.VALUE_CATEGORY_NAME);
            int categoryID = Integer.parseInt(string_categoryID);
            
            if(categoryName == null || CommonConstants.BLANK.equals(categoryName)) {
                CategoryLogic cateLogic = new CategoryLogicImpl();
                CategoryDaoBean cateBean = cateLogic.loadCategoryByID(categoryID);
                categoryName = cateBean.getCategoryName();
            }
                   
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            List<ShoesDaoBean> shoesList = shoesLogic.loadShoesNewArrivals(categoryID, "5/11/2013");
            
            if (shoesList != null) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_SHOES_CATEGORY, shoesList);
                request.setAttribute(CommonConstants.REQUEST_CATEGORY_NAME, categoryName);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.NEW_ARRIVALS_PAGE);
                rd.forward(request, response);
            }
        } else if (CommonConstants.LOAD_SEX_CATEGORY.equals(btAction)) {
            String string_categoryID = request.getParameter(CommonConstants.VALUE_CATEGORY_ID);
            String categoryName = request.getParameter(CommonConstants.VALUE_CATEGORY_NAME);
            String sex = request.getParameter(CommonConstants.VALUE_SEX_LOAD_CATEGORY);
            int categoryID = Integer.parseInt(string_categoryID);
            
            if(categoryName == null || CommonConstants.BLANK.equals(categoryName)) {
                CategoryLogic cateLogic = new CategoryLogicImpl();
                CategoryDaoBean cateBean = cateLogic.loadCategoryByID(categoryID);
                categoryName = cateBean.getCategoryName();
            }
            
            ShoesLogic shoesLogic = new ShoesLogicImpl();

            List<ShoesDaoBean> shoesList = shoesLogic.loadShoesbyCategoryIDAndSex(categoryID, sex);

            if (shoesList != null) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_SHOES_CATEGORY, shoesList);
                request.setAttribute(CommonConstants.REQUEST_CATEGORY_NAME, categoryName);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.MENS_PAGE);
                rd.forward(request, response);
            }

        } else if (CommonConstants.VIEW_SALES_ITEM.equals(btAction)) {
            ShoesLogic shoesLogic = new ShoesLogicImpl();
            List<ShoesDaoBean> shoesList = shoesLogic.loadAllSaleItems();

            if (shoesList != null) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_SHOES_CATEGORY, shoesList);
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.SALE_ITEMS_PAGE);
                rd.forward(request, response);
            }
        } else if (CommonConstants.SEND_MESSAGE.equals(btAction)) {
            String email = request.getParameter(CommonConstants.VALUE_EMAIL_CONTACT);
            String title = request.getParameter(CommonConstants.VALUE_TITLE_CONTACT);
            String content = request.getParameter(CommonConstants.VALUE_CONTENT_CONTACT);

            ContactLogic contLogic = new ContactLogicImpl();
            boolean result = contLogic.sendContact(title, email, content);

            RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CONTACT_PAGE);
            rd.forward(request, response);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub

        String btAction = request.getParameter(CommonConstants.VALUE_HIDDEN_BUTTON);
        if (CommonConstants.LOGIN.equals(btAction)) {
            String email = request.getParameter(CommonConstants.VALUE_EMAIL);
           
            String password = request.getParameter(CommonConstants.VALUE_PASSWORD);
            System.out.println(email + " - " + password);
            String page = request.getParameter(CommonConstants.VALUE_RETURN_LOGIN_PAGE);
            UsersLogic userBean = new UsersLogicImpl();
            boolean result = userBean.checkLogin(email, password);
            UsersDaoBean user = userBean.loadUsersByEmail(email);
            boolean isAdmin = user.isAdmin();
            if (isAdmin && result) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.EMAIL, email);
                RequestDispatcher rd = request.getRequestDispatcher("homepage.jsp");
                rd.forward(request, response);
           
            } else if (result) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.EMAIL, email);
                RequestDispatcher rd = request.getRequestDispatcher(page);
                rd.forward(request, response);
            } 
                else {
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.LOGINFAIL);
                rd.forward(request, response);
            }
        } else if (CommonConstants.REGISTER.equals(btAction)) {
            String email = request.getParameter(CommonConstants.VALUE_REGISTER_EMAIL);
            String password = request.getParameter(CommonConstants.VALUE_REGISTER_PASSWORD);
            String rePassword = request.getParameter(CommonConstants.VALUE_RE_PASSWORD);
            String lastName = request.getParameter(CommonConstants.VALUE_LAST_NAME);
            String firstName = request.getParameter(CommonConstants.VALUE_FIRST_NAME);
            String address = request.getParameter(CommonConstants.VALUE_ADDRESS);
            String string_phone = request.getParameter(CommonConstants.VALUE_PHONE);
            int phoneNumber = Integer.parseInt(string_phone);
            String sex = request.getParameter(CommonConstants.VALUE_SEX);
            String birthday = request.getParameter(CommonConstants.VALUE_BIRTH_DAY);
            boolean admin = false;
            UsersLogic userBean = new UsersLogicImpl();
            boolean result = userBean.registerAccount(email, password, sex, birthday, firstName, lastName, admin,
                    address, phoneNumber);
            if (result) {
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.HOMEPAGE);
                rd.forward(request, response);
            }
        } else if (CommonConstants.SEARCH_COLOR_NAME.equals(btAction)) {

            String shoesName = request.getParameter(CommonConstants.VALUE_SEARCH_SHOES);
            String shoesColor = request.getParameter(CommonConstants.VALUE_COLOR);

            ShoesLogic shoesLogic = new ShoesLogicImpl();
            List<ShoesDaoBean> shoesList = shoesLogic.searchShoes(shoesName, shoesColor);

            if (shoesList != null) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.SESSION_SHOES_CATEGORY, shoesList);

                RequestDispatcher rd = request.getRequestDispatcher("search-shoes.jsp");
                rd.forward(request, response);
            }
        } else if (CommonConstants.SEND_MESSAGE.equals(btAction)) {
            String email = request.getParameter(CommonConstants.VALUE_EMAIL_CONTACT);
            String title = request.getParameter(CommonConstants.VALUE_TITLE_CONTACT);
            String content = request.getParameter(CommonConstants.VALUE_CONTENT_CONTACT);

            ContactLogic contLogic = new ContactLogicImpl();
            boolean result = contLogic.sendContact(title, email, content);

            RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.CONTACT_PAGE);
            rd.forward(request, response);
        } else if ("LoginCheckOut".equals(btAction)) {
            String email = request.getParameter(CommonConstants.VALUE_EMAIL);
            
            String password = request.getParameter(CommonConstants.VALUE_PASSWORD);
            String page = request.getParameter(CommonConstants.VALUE_RETURN_LOGIN_PAGE);
            UsersLogic userBean = new UsersLogicImpl();
            boolean result = userBean.checkLogin(email, password);
            UsersDaoBean user = userBean.loadUsersByEmail(email);
            boolean isAdmin = user.isAdmin();
            if (isAdmin && result) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.EMAIL, email);
                RequestDispatcher rd = request.getRequestDispatcher("homepage.jsp");
                rd.forward(request, response);
           
            } else if (result) {
                HttpSession session = request.getSession();
                session.setAttribute(CommonConstants.EMAIL, email);
                String abc = "CartAction?btAction=CheckOut&txtEmail=" + email;
                System.out.println(abc);
//                RequestDispatcher rd = request.getRequestDispatcher("CartAction?btAction=CheckOut&txtEmail=" + email);
//                rd.forward(request, response);
                response.sendRedirect("CartAction?btAction=CheckOut&txtEmail=" + email);
            } 
                else {
                RequestDispatcher rd = request.getRequestDispatcher(CommonConstants.LOGINFAIL);
                rd.forward(request, response);
            }
        }
    }

}
